<?php


/**
 * this class provides some useful functions for handling languages in 
 * application
 * 
 * @author		simon ungerboeck <ungerboeck_simon@hotmail.com>
 * @version		1.0
 */

class Language {
	private $sqlObj;
	public function __construct($sqlObj){
		$this->sqlObj = $sqlObj;
	}


	/**
	 * returns the text by given key and language
	 * if language is not set, use standard language
	 * 
	 * @param		string		$language_key
	 * @param		string		$language
	 * @return		string		$langText
	 */
	public static function getTextByLanguage($language_key, $language = null) {
		$language = ($language != null) ? $language : LANGUAGE;
		$langText = null;
		try {
			$sql = 'select language_value from ' . DB_CORE . '.language_view where upper(language_key)=upper(?) ';
			$sql .= 'and upper(language)=upper(?) and application_id=?';
			$params = array(
					$language_key, 
					$language, 
					APPLICATION_ID);
			
			$sqlObj = Sql_Object::getInstance();
			$sqlObj->connect();
			$sqlObj->execute($sql, $params);
			$row = $sqlObj->fetch();
			$langText = $row['language_value'];
			$sqlObj->disconnect();
		} catch ( Exception $e ) {
			$langText = $e->getMessage();
		}
		return $langText;
	}
	
	
	/**
	 * registrates a new language key
	 * @param		string		$languageKey
	 * @param		int			$applicationId
	 */
	public function registrateKey($languageKey, $applicationId){
		$sql = 'insert into ' . DB_CORE . '.language_key (language_key, application_id) values (?, ?)';
		$params = array ($languageKey, $applicationId);
		$this->sqlObj->execute($sql, $params);
		return $this->sqlObj->getLastRecordId();
	}
	
	
	/**
	 * registrates a new language value to given language key an language
	 * @param		string 		$languageValue
	 * @param		int			$languageKeyId
	 * @param		string		$language/
	 */
	public function registrateValue($languageValue, $languageKeyId, $language){
		$sql = 'insert into ' . DB_CORE . '.language_value (language_value, language_key_id, language_id) values (?, ?, (select language_id from ' . DB_CORE . '.language where upper(name) = upper(?)))';
		$params = array($languageValue, $languageKeyId, $language);
		$this->sqlObj->execute($sql, $params);
	}
	
	
	/**
	 * updates the language value by given languageValueId
	 * @param		string		$languageValue
	 * @param		int			$languageValueId
	 */
	public function updateValue($languageValue, $languageValueId){
		$sql = 'update ' . DB_CORE . '.language_value set language_value = ? where language_value_id = ?';
		$params = array($languageValue, $languageValueId);
		$this->sqlObj->execute($sql, $params);
	}
}
?>